package com.vanysoft.business.example;

import com.vanysoft.business.index.action.demo.LoginDemo;
import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.framework.base.action.BaseController;
import com.vanysoft.framework.config.mvc.SessionUser;
import com.vanysoft.framework.exception.VanySoftBaseException;
import com.vanysoft.framework.messagesource.MessageCodeConstant;
import com.vanysoft.framework.redis.RedisService;
import com.vanysoft.framework.utils.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 类名称：ExampleController
 * 类描述：例子
 * 创建人：SharpClaw_x
 * 创建时间：2017年5月19日 上午10:42:31
 * 修改人：SharpClaw_x
 * 修改时间：2017年5月19日 上午10:42:31
 * 版本信息：@version 1.0
 * Copyright (c) 2017SharpClaw-版权所有
 * 修改备注：
 */
@RestController
@RequestMapping("/example")
public class ExampleController extends BaseController {

    @Autowired
    RedisService redisService;

    /**
     * Function Name               redisLogin
     *
     * @param loginDemo
     * @return Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月19日 上午10:45:25           SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/redisLogin", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public JSONResponse redisLogin(LoginDemo loginDemo) {
        String userId = loginDemo.getUserAccount();
        String password = loginDemo.getPassword();

        String respcode = "000000"; // userId = pwd 验证通过
        UserInfo userInfo = new UserInfo();
        String token = UUID.randomUUID().toString();
        if ("000000".equals(respcode)) {
            userInfo.setId(userId);
            userInfo.setPassword(password);
            List<String> permissions = new ArrayList<>();
            permissions.add("/test");
            redisService.setUserInfo(token, userInfo);
        }
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("userInfo", userInfo);
        dataMap.put("token", token);


        return new JSONResponse(respcode, dataMap);
    }

    /**
     * Function Name               testGetUserInfo
     *
     * @return
     * @throws VanySoftBaseException
     * @description 获取当前登录人的基础信息
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月19日 上午10:46:09           SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/testGetUserInfo")
    public JSONResponse testGetUserInfo(@SessionUser UserInfo userInfo) throws VanySoftBaseException {

        return new JSONResponse("testGetUserInfo", userInfo);
    }


    /**
     * Function Name               throwException
     *
     * @param request
     * @return
     * @throws VanySoftBaseException
     * @description 在 action-service-dao自定义异常，被捕获
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月19日 上午10:52:03           SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/throwException")
    public JSONResponse throwException(HttpServletRequest request) throws VanySoftBaseException {
        try {
            Integer.valueOf("uuu");
        } catch (Exception e) {
            throw new VanySoftBaseException(MessageCodeConstant.RTN_CODE_UNKNOW_ERROR);
        }

        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS);
    }

    /**
     * Function Name               returnJson
     *
     * @param request
     * @return
     * @throws VanySoftBaseException
     * @description 返回Json格式数据
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月19日 上午10:57:37           SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/returnJson")
    public JSONResponse returnJson(HttpServletRequest request) throws VanySoftBaseException {
        List<String> list1 = new ArrayList<>();
        list1.add("s1");
        list1.add("s2");
        list1.add("s3");
        list1.add("s4");
        return new JSONResponse(list1);
    }

    /**
     * Function Name               returnJson2
     *
     * @param request
     * @return
     * @throws VanySoftBaseException
     * @description 返回Json 格式数据
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月19日 上午10:57:53           SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/returnJson2")
    public JSONResponse returnJson2(HttpServletRequest request) throws VanySoftBaseException {
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> userMap1 = new HashMap<>();
        userMap1.put("1", "1-1");
        userMap1.put("2", "2-1");
        Map<String, String> userMap2 = new HashMap<>();
        userMap2.put("1", "1-2");
        userMap2.put("2", "2-2");
        list.add(userMap1);
        list.add(userMap2);
        return new JSONResponse("returnJson2", list);
    }


    @RequestMapping(value = "/test", produces = "application/json;charset=UTF-8")
    public JSONResponse test(HttpServletRequest request) throws Exception {
//		Integer.valueOf("2d");
//		
//		System.err.println("test-id:"+request.getParameter("id"));

        HttpSession session = request.getSession();
        if (session.isNew()) {
            System.err.println("new Session:" + session.getId());
            session.setAttribute("shuxl", "shuxl-value:" + UUID.randomUUID().toString());
        } else {
            System.err.println("old Session:" + session.getId());
        }
        return new JSONResponse("000000", String.valueOf(session.getAttribute("shuxl")));
    }


    @RequestMapping(value = "/testRuntimeException", produces = "application/json;charset=UTF-8")
    public JSONResponse RuntimeException(HttpServletRequest request) {

        return new JSONResponse("000000");
    }

    @RequestMapping(value = "/testVanyException", produces = "application/json;charset=UTF-8")
    public JSONResponse testVanyException(HttpServletRequest request) throws VanySoftBaseException {
        try {
            Integer.valueOf("dfaskdfjka");
        } catch (Exception e) {
            System.err.println(e.getMessage());
            throw new VanySoftBaseException("testCode", "errorMsg----", e.getCause());
        }

        return new JSONResponse("000000");
    }

    @RequestMapping(value = "/testMsg", produces = "application/json;charset=UTF-8")
    public JSONResponse testMsg(HttpServletRequest request) throws VanySoftBaseException {

        return new JSONResponse("testCode");
    }

    @RequestMapping(value = "/testApi", produces = "application/json;charset=UTF-8")
    public JSONResponse testMsg(HttpServletRequest request, String userId, Integer inti) throws VanySoftBaseException {

        return new JSONResponse("testApi");
    }

    @RequestMapping(value = "/testLoginApi", produces = "application/json;charset=UTF-8")
    public JSONResponse testLoginApi(HttpServletRequest request, String userId, Integer inti) throws VanySoftBaseException {
        return new JSONResponse("testLoginApi");
    }

    @RequestMapping(value = "/testJsonApi1")
    public JSONResponse testJsonApi1(HttpServletRequest request) throws VanySoftBaseException {

        return new JSONResponse("testLoginApi1");
    }

    @RequestMapping(value = "/testJsonApi2")
    public JSONResponse testJsonApi2(HttpServletRequest request) throws VanySoftBaseException {
        List<String> list1 = new ArrayList<>();
        list1.add("s1");
        list1.add("s2");
        list1.add("s3");
        list1.add("s4");
        return new JSONResponse("testLoginApi2", list1);
    }

    @RequestMapping(value = "/testJsonApi3")
    public JSONResponse testJsonApi3(HttpServletRequest request) throws VanySoftBaseException {
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> userMap1 = new HashMap<>();
        userMap1.put("1", "1-1");
        userMap1.put("2", "2-1");
        Map<String, String> userMap2 = new HashMap<>();
        userMap2.put("1", "1-2");
        userMap2.put("2", "2-2");
        list.add(userMap1);
        list.add(userMap2);
        return new JSONResponse("testLoginApi3", list);
    }

}
